<template>
  <div class="login-container">
    <div class="login-wrapper">
      <!-- 系统标题 -->
      <div class="login-header">
        <img :src="logo" alt="none" class="logo">
        <div class="split" />
        <div class="sub-title">
          <div class="system-cn-title">商品运营管理系统</div>
          <div class="system-en-name">Commodity Operation Management Center</div>
        </div>
      </div>
      <!-- 登录框 -->
      <div class="login-content">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="off"
        >
          <!-- 登录框标题 -->
          <div class="title-container">
            <h3 class="title">账号登录</h3>
          </div>
          <!-- 用户名 -->
          <el-form-item prop="username">
            <div class="form-item-container">
              <svg-icon class="svg-container" icon-class="user" />
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="请输入用户名"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </div>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <div class="form-item-container">
              <svg-icon class="svg-container" icon-class="password" />
              <el-input
                ref="password"
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                show-password
                @keyup.enter.native="handleLogin"
              />
            </div>
          </el-form-item>
          <!-- 登录按钮 -->
          <el-button
            :loading="loading"
            class="login-btn"
            type="primary"
            @click.native.prevent="handleLogin"
          >登录</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import logo from '@/assets/img/logo.png';
export default {
  name: 'Login',
  data() {
    return {
      logo: logo,
      loading: false,
      redirect: undefined,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '请输入长度为6~16位的字符组合' }
        ]
      }
    };
  },
  watch: {
    $route: {
      immediate: true,
      deep: true,
      handler({ query }) {
        this.redirect = query && query.redirect;
      }
    }
  },
  methods: {
    handleLogin() {
      this.loading = true;
      this.$refs.loginForm.validate((valid) => {
        if (!valid) return false;
        this.$store
          .dispatch('user/login', this.loginForm)
          .then(() => {
            this.loading = false;
            this.$router.push({ path: this.redirect || '/' });
          })
          .catch(() => {
            this.loading = false;
          });
      });
    }
  }
};
</script>

<style lang="scss">
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  font-family: PingFang SC Semibold, PingFang SC Semibold-Semibold;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url("~@/assets/img/login_bg.png");
  background-repeat: no-repeat;
  background-size: cover;
  .el-input {
    display: inline-block;

    input {
      background: transparent;
      -webkit-appearance: none;
      border-radius: 5px;
      border: 1px solid #e4e7ed;
      padding: 12px 5px 12px 15px;
      color: #454545;
      height: 40px;
      caret-color: #909399;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
.forget_password {
  color: #fff;
  cursor: pointer;
}

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #2d3a4b;
  overflow: hidden;
  .login-wrapper {
    width: 100%;
    height: 100%;
    min-width: 1280px;
    position: relative;
    .login-header {
      display: flex;
      align-items: center;
      min-height: 70px;
      height: 18vh;
      padding-left: 70px;
      font-size: 30px;
      color: #014fa7;
      .split {
        height: 54px;
        background-color: #fff;
        margin: 0 20px;
        width: 2px;
      }
      .sub-title {
        font-family: PingFangSC, PingFangSC-Medium;
        font-weight: 500;
        text-align: left;
        color: #fff;
        .system-cn-title {
          width: 256px;
          height: 45px;
          font-size: 32px;
          line-height: 45px;
        }
        .system-en-name {
          width: 300px;
          height: 20px;
          font-size: 14px;
          line-height: 20px;
        }
      }
      .logo {
        display: inline-block;
        height: 54px;
      }
    }

    .login-content {
      min-height: 450px;
      height: 68.9vh;
      position: relative;
      .login-logo {
        width: 400px;
        height: 400px;
        position: absolute;
        right: 40%;
        top: 50%;
        -webkit-transform: translateY(-50%);
        transform: translateY(-50%);
      }
      .login-form {
        position: absolute;
        right: 10%;
        top: 50%;
        padding: 30px;
        -webkit-transform: translateY(-50%);
        transform: translateY(-50%);
        width: 22.27%;
        max-width: 482px;
        background-color: #fff;
        border-radius: 4px;
      }
      .login-btn {
        width: 100%;
        margin: 20px 0;
        color: #fff;
        background-color: #0d59ff !important;
        border-color: #0d59ff !important;
      }
      .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
          &:first-of-type {
            margin-right: 16px;
          }
        }
      }

      .title-container {
        position: relative;
        .title {
          font-size: 26px;
          color: #3877ff;
          line-height: 80px;
          text-align: left;
          font-weight: 900;
        }
      }
      .form-item-container {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        .svg-container {
          display: inline-block;
          width: 28px;
          height: 28px;
          padding: 6px;
          margin-right: 10px;
          color: #889aa4;
          vertical-align: middle;
          display: inline-block;
        }
      }
    }
  }
}
</style>
